package com.yuma.springaichatmodel.controller;

import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.model.Generation;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.SystemPromptTemplate;
import org.springframework.ai.deepseek.DeepSeekAssistantMessage;
import org.springframework.ai.deepseek.DeepSeekChatModel;
import org.springframework.ai.deepseek.DeepSeekChatOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
public class DeepSeekChatModelController {

    @Autowired
    private DeepSeekChatModel deepSeekChatModel;

    @GetMapping("/deep/chat")
    public String chat(@RequestParam(value="msg", defaultValue = "你好") String message){
        ChatResponse response = deepSeekChatModel.call(
                new Prompt(
                        message,  // 客户提问的内容
                        ChatOptions.builder()   // 选项
                                .model("deepseek-chat") // 可设置大模型
                                .temperature(0.7)  // 多样性
                                .build()
                )
        );
        return response.getResult().getOutput().getText();
    }

    @GetMapping(value="/deep/call", produces = "text/html;charset=utf-8")
    public String call(@RequestParam(value="msg", defaultValue = "你好") String message){
        return deepSeekChatModel.call(message);
    }

    @GetMapping(value="/deep/stream", produces = "text/html;charset=utf-8")
    public Flux<String> stream(@RequestParam(value="msg", defaultValue = "你好") String message){
        return deepSeekChatModel.stream(message);
    }

    // 温度
    @GetMapping("/deep/temperature/{msg}")
    public String temperature(@PathVariable String msg){
        DeepSeekChatOptions options = DeepSeekChatOptions.builder().temperature(0.3).build();
        ChatResponse response = deepSeekChatModel.call(new Prompt(msg, options));
        return response.getResult().getOutput().getText();
    }

    // maxtoken
    @GetMapping("/deep/maxtoken/{msg}")
    public String maxtoken(@PathVariable String msg){
        DeepSeekChatOptions options = DeepSeekChatOptions.builder()
                .maxTokens(10)
                .build();
        ChatResponse response = deepSeekChatModel.call(new Prompt(msg, options));
        return response.getResult().getOutput().getText();
    }

    // stop
    @GetMapping("/deep/stop/{msg}")
    public String stop(@PathVariable String msg){
        DeepSeekChatOptions options = DeepSeekChatOptions.builder()
                //.stop(List.of("。"))
                .build();
        ChatResponse response = deepSeekChatModel.call(new Prompt(msg, options));
        return response.getResult().getOutput().getText();
    }

    // 设置模型
    @GetMapping("/deep/reasoning")
    public String reasoning() {
        DeepSeekChatOptions options = DeepSeekChatOptions.builder()
                .model("deepseek-reasoner").build();
        // 提示词对象
        Prompt prompt = new Prompt("请描写一句赞美国家的诗句。", options);

        ChatResponse res = deepSeekChatModel.call(prompt);
        DeepSeekAssistantMessage assistantMessage =  (DeepSeekAssistantMessage)res.getResult().getOutput();
        // 大模型的推理过程
        String reasoningContent = assistantMessage.getReasoningContent();
        return "推理过程:" + reasoningContent + "<br/>" + "响应:" + assistantMessage.getText();
    }

    @GetMapping("/deep/prompt")
    public String prompt(@RequestParam(value="name") String name,@RequestParam("check") String check ,@RequestParam(value="hobby") String hobby){
        // 设置用户输入信息
        UserMessage userMessage = new UserMessage(check);
        // 使用PromptTemplate设置信息
        SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate("你是一个美食咨询助手，可以帮助人们查询美食信息。你得名字是{name}，你应该用你的名字和{hobby}的饮食习惯回复用户的请求。");
        // 替换占位符
        Message systemMessage = systemPromptTemplate.createMessage(Map.of("name", name, "hobby", hobby));
        // 提示词：用户提问和系统身份
        Prompt prompt = new Prompt(List.of(userMessage, systemMessage));
        // 使用chatModel方法
        List<Generation> results = deepSeekChatModel.call(prompt).getResults();
        return results.stream().map(x->x.getOutput().getText()).collect(Collectors.joining(" "));
    }

    @GetMapping("/deep/prompt2")
    public String prompt2(@RequestParam(value="name") String name,
                          @RequestParam("check") String check ,
                          @RequestParam(value="hobby") String hobby,
                          @Value("classpath:/prompts.st") Resource systemResource){
        // 设置用户输入信息
        UserMessage userMessage = new UserMessage(check);
        // 使用PromptTemplate设置信息
        SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemResource);
        // 替换占位符
        Message systemMessage = systemPromptTemplate.createMessage(Map.of("name", name, "hobby", hobby));
        // 提示词：用户提问和系统身份
        Prompt prompt = new Prompt(List.of(userMessage, systemMessage));
        // 使用chatModel方法
        List<Generation> results = deepSeekChatModel.call(prompt).getResults();
        return results.stream().map(x->x.getOutput().getText()).collect(Collectors.joining(" "));
    }


}
